package cn.sc.allinpay.allfavour.business.cashiertrans;

import android.os.Bundle;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.AppCompatSpinner;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.TextView;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.pai.common.adapter.BaseRecyAdapter;
import cn.sc.allinpay.allfavour.R;
import cn.sc.allinpay.allfavour.base.activity.BaseActivity;
import cn.sc.allinpay.allfavour.base.widget.DecorationVert;
import cn.sc.allinpay.allfavour.business.common.adapter.CashierTransAdapter;
import cn.sc.allinpay.allfavour.business.common.data.TransRecord;


/**
 * activity 交易流水
 * author：pany
 * on 2017/12/1 11:14
 */
public class CashierTransActivity extends BaseActivity<CashierTransContract.View, CashierTransContract.Presenter>
        implements CashierTransContract.View, AdapterView.OnItemSelectedListener {

    @BindView(R.id.tvTransAmt)
    AppCompatTextView tvTransAmt;//消费总金额
    @BindView(R.id.spDate)
    AppCompatSpinner spDate;
    @BindView(R.id.spTransType)
    AppCompatSpinner spTransType;
    @BindView(R.id.etOrderNo)//订单号
    AppCompatEditText etOrderNo;
    @BindView(R.id.llFilter)//筛选布局
    View llFilter;
    @BindView(R.id.llInput)//输入布局
    View llInput;
    @BindView(R.id.recycler)
    RecyclerView recycler;

    private CashierTransAdapter transAdapter;

    @Override
    protected int layoutId() {
        return R.layout.activity_cashier_trans;
    }

    @Override
    protected void layoutTitle(View top, AppCompatTextView left, AppCompatTextView title, AppCompatTextView right) {
        title.setText("交易流水");
    }

    @Override
    protected CashierTransContract.Presenter bindPresenter() {
        return new CashierTransPresenter();
    }

    @Override
    protected void layoutContent(Bundle savedInstanceState) {
        final InputMethodManager manager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        etOrderNo.setOnEditorActionListener(new TextView.OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    presenter.queryTransRecords();
                }
                return false;
            }
        });
        spDate.setOnItemSelectedListener(this);
        spTransType.setOnItemSelectedListener(this);
        recycler.addItemDecoration(new DecorationVert(this, R.dimen.margin_common, R.dimen.margin_common));
        recycler.setLayoutManager(new LinearLayoutManager(this));// 列表布局
        transAdapter = new CashierTransAdapter();
        transAdapter.setOnAdapterListener(new BaseRecyAdapter.OnAdapterListener<TransRecord>() {
            @Override
            public void onItemClick(View view, int position, TransRecord data) {
                presenter.startTransDetailActivity(data);
            }
        });
        recycler.setAdapter(transAdapter);
    }

    @Override
    public void showCondition(int visible) {
        llFilter.setVisibility(visible);
        llInput.setVisibility(visible);
    }

    @Override
    public void showTransAmt(String data) {
        tvTransAmt.setText("消费总额: " + data);
    }

    @Override
    public int getDate() {
        return spDate.getSelectedItemPosition();
    }

    @Override
    public int getTransType() {
        return spTransType.getSelectedItemPosition();
    }

    @Override
    public String getOrderNo() {
        return etOrderNo.getText().toString().trim();
    }

    @Override
    public void transAdapterChange(List<TransRecord> datas) {
        transAdapter.change(datas);
    }

    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        presenter.queryTransRecords();
    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {

    }

    @OnClick({R.id.btnQry})
    public void btnClick(View view) {
        switch (view.getId()){
            case R.id.btnQry:
                presenter.queryTransRecords();
                break;
        }
    }
}
